package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdchannelMapper;
import com.usian.admin.service.AdchannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdchannelServiceImpl extends ServiceImpl<AdchannelMapper, AdChannel> implements AdchannelService {
    @Override
    public ResponseResult findPage(ChannelDto dto) {

        //参数校验
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //分页参数校验
        dto.checkParam();

        //分页名称模糊查询
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
        }
        if (StringUtils.isNotBlank(dto.getSelectState())){
            lambdaQueryWrapper.like(AdChannel::getStatus,dto.getSelectState());
        }
        IPage iPage = page(page, lambdaQueryWrapper);

        //封装结果
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal());
        responseResult.setData(iPage.getRecords());

        return responseResult;
    }

    @Override
    public ResponseResult addChannel(AdChannel adChannel) {

        //参数校验
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //添加当前时间为创建时间
        adChannel.setCreatedTime(new Date());
        boolean b = save(adChannel);

        //判断是否操作成功
        if (b){
            //成功返回
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            //失败返回
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseResult updateAdchannel(AdChannel adChannel) {

        //参数校验
        if (adChannel==null || adChannel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.修改
        boolean b = updateById(adChannel);

        //判断是否操作成功
        if (b){
            //成功返回
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            //失败返回
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseResult delById(Integer id) {

        //参数校验
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdChannel adChannel = this.getById(id);

        //判断数据是否存在
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断数据是否有效
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        //删除频道
        removeById(id);

        //返回状态码
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
